package com.yn.iotxiao.controller.Group;

import com.alibaba.fastjson.JSONObject;
import com.yn.iotxiao.Utils.JsonHelper;
import com.yn.iotxiao.Utils.StringUtil;
import com.yn.iotxiao.authorize.Authorize;
import com.yn.iotxiao.request.PageRequest;
import com.yn.iotxiao.response.ResponseResult;
import com.yn.iotxiao.sql.entity.Device.DeviceInfo;
import com.yn.iotxiao.sql.entity.Group.GroupDevice;
import com.yn.iotxiao.sql.entity.Group.GroupInfo;
import com.yn.iotxiao.sql.services.Group.GroupService;
import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@RequestMapping("/group")
@Slf4j
@RestController
@ResponseResult
public class GroupController {

    @Autowired
    GroupService service;

    /**
     * 写入分组
     * @param request 分组信息
     * @return
     * @throws Exception
     */
    @PutMapping
    @Authorize
    public String put(@Validated @RequestBody GroupInfo request) throws Exception{

        if(StringUtils.isBlank(request.getId())) {
            request.setId(StringUtil.getUUID());
        }
        int result = service.insert(request);
        if (result > 0) {
            return  "保存成功";
        }
        throw new Exception("保存失败");
    }

    /**
     * 条件查询分组信息
     * @param request 分页查询
     * @return
     */
    @PostMapping("/list")
    @Authorize
    public Object getList(@RequestBody PageRequest request) {
        var list = service.getListPage(request.getSearchKey(), request.getPage(), request.getPageSize());
        var total = service.getTotal(request.getSearchKey());

      return JsonHelper.create("total",total,"list",list);
    }

    /**
     * 写入设备到分组中
     * @param deviceIds 设备ID
     * @param groupId 分组ID
     * @return
     * @throws Exception
     */
    @PutMapping("/device/{groupId}")
    @Authorize
    public String putDevice(@RequestBody List<String> deviceIds, @PathVariable String groupId) throws Exception{

        List<GroupDevice> list = new ArrayList<>();
        for (var deviceId:
                deviceIds ) {
            GroupDevice device = new GroupDevice();
            device.setGroupid(groupId);
            device.setDeviceid(deviceId);
            list.add(device);
        }
        int result = service.putDevice(list);

        if (result > 0) {
            return  "设置成功";
        }
        throw new Exception("设置失败");
    }


    /**
     * 不在分组中的设备
     * @param groupId 分组ID
     * @return
     */
    @GetMapping("/device/group/notin/{groupId}")
    @Authorize
    public List<DeviceInfo> getDeviceNotInGroup(@PathVariable String groupId) {

        return service.getDeviceOutGroup(groupId);
    }


    /**
     * 条件查询分组中的设备信息
     * @param groupId 分组ID
     * @param request 分页查询
     * @return
     */
    @PostMapping("/device/group/in/{groupId}")
    @Authorize
    public JSONObject getDeviceInGroup(@PathVariable String groupId,@RequestBody PageRequest request) {
        var list = service.getDeviceInGroup(groupId,request.getSearchKey(), request.getPage(), request.getPageSize());
        var total = service.getDeviceInGroupTotal(groupId);
        return JsonHelper.create("total",total,"list",list);
    }

    @PostMapping("/device/group/remove/{groupId}")
    @Authorize
    public String removeDeviceFromGroup(@PathVariable String groupId, @RequestBody List<String> deviceIds) throws Exception{

        int result = service.removeDeviceFromGroup(groupId,deviceIds);
        if (result > 0) {
            return  "移除设备";
        }
        throw new Exception("移除失败");
    }

    /**
     * 查询权限组下的人员信息
     * @param groupId 分组ID
     * @param request 分页查询
     * @return
     */
    @PostMapping("/user/group/in/{groupId}")
    @Authorize
    public JSONObject getUserInGroup(@PathVariable String groupId, @RequestBody PageRequest request) {

        return service.getUserInGroup(groupId,request.getSearchKey(), request.getPage(), request.getPageSize());
    }
}
